package gui.CalendarTab;
import java.util.Calendar;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.events.*;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Table;

/**
 * class CalendarTab.
 * the content of the calendar tab. contains the calendar and the viewing options
 * @author michal
 *
 */
public class CalendarTab extends Composite {
	private UpdateCalendarGUI updateCalendarGUI;
	private Table table;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CalendarTab(Composite parent, int style) {
		super(parent, style);
		setLayout(new FormLayout());
		
		final DateTime calendar = new DateTime(this,  SWT.CALENDAR | SWT.RIGHT_TO_LEFT);
		FormData fd_dateTime = new FormData();
		fd_dateTime.top = new FormAttachment(0, 10);
		fd_dateTime.right = new FormAttachment(100, -10);
		calendar.setLayoutData(fd_dateTime);
		final Button dayButton = new Button(this, SWT.RADIO | SWT.RIGHT_TO_LEFT);
		dayButton.setAlignment(SWT.CENTER);
		FormData fd_dayButton = new FormData();
		fd_dayButton.left = new FormAttachment(100, -138);
		fd_dayButton.top = new FormAttachment(calendar, 14);
		fd_dayButton.right = new FormAttachment(100, -87);
		dayButton.setLayoutData(fd_dayButton);
		dayButton.setText("\u05D9\u05D5\u05DD");
		dayButton.setSelection(true);
		
		final Button weekButton = new Button(this, SWT.RADIO | SWT.RIGHT_TO_LEFT);
		weekButton.setAlignment(SWT.CENTER);
		FormData fd_weekButton = new FormData();
		fd_weekButton.top = new FormAttachment(dayButton, 6);
		fd_weekButton.right = new FormAttachment(100, -87);
		weekButton.setLayoutData(fd_weekButton);
		weekButton.setText("\u05E9\u05D1\u05D5\u05E2");
		
		final Button monthButton = new Button(this, SWT.RADIO | SWT.RIGHT_TO_LEFT);
		monthButton.setAlignment(SWT.CENTER);
		FormData fd_monthButton = new FormData();
		fd_monthButton.top = new FormAttachment(weekButton, 6);
		fd_monthButton.right = new FormAttachment(100, -87);
		monthButton.setLayoutData(fd_monthButton);
		monthButton.setText("\u05D7\u05D5\u05D3\u05E9");
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		table.setVisible(true);
		FormData fd_table = new FormData();
		fd_table.left = new FormAttachment(0, 26);
		fd_table.right = new FormAttachment(calendar, -15);
		fd_table.top = new FormAttachment(calendar, 0, SWT.TOP);
		fd_table.bottom = new FormAttachment(0, 571);
	
		table.setLayoutData(fd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(false);
		
		
		
		int day = calendar.getDay();
		int month = calendar.getMonth();
		int year = calendar.getYear();
		Calendar c = Calendar.getInstance();
		c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
		updateCalendarGUI = new UpdateCalendarGUI(table,day,month,year,dayOfWeek);
		/**
		 * once user selects a day - show the day in the table
		 */
		calendar.addSelectionListener (new SelectionAdapter () {
			public void widgetSelected (SelectionEvent e) {
				//checking which display to show - day \ week \ month
				int day = calendar.getDay();
				int month = calendar.getMonth();
				int year = calendar.getYear();
				if (dayButton.getSelection()){					
					Calendar c = Calendar.getInstance();
					c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
					int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
					updateCalendarGUI.updateCalendar(CalendarOption.DAY, day, month, year, null, dayOfWeek);
				}
				else if (weekButton.getSelection()){					
					Calendar c = Calendar.getInstance();
					c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
					int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
					updateCalendarGUI.updateCalendar(CalendarOption.WEEK, day, month, year, null, dayOfWeek);
				}else if (monthButton.getSelection()){					
					Calendar c = Calendar.getInstance();
					c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
					int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
					updateCalendarGUI.updateCalendar(CalendarOption.MONTH, day, month, year, null, dayOfWeek);
				}
			}
		});

		dayButton.addMouseListener(new MouseListener() {			
			@Override
			public void mouseUp(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}			
			@Override
			public void mouseDown(MouseEvent arg0) {
				int day = calendar.getDay();
				int month = calendar.getMonth();
				int year = calendar.getYear();
				
				Calendar c = Calendar.getInstance();
				c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
				int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
				updateCalendarGUI.updateCalendar(CalendarOption.DAY, day, month, year, null, dayOfWeek);				
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				// TODO Auto-generated method stub
			}
		});
		
		weekButton.addMouseListener(new MouseListener() {			
			@Override
			public void mouseUp(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				int day = calendar.getDay();
				int month = calendar.getMonth();
				int year = calendar.getYear();
				
				Calendar c = Calendar.getInstance();
				c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
				int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
				updateCalendarGUI.updateCalendar(CalendarOption.WEEK, day, month, year, null, dayOfWeek);
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
		});

		monthButton.addMouseListener(new MouseListener() {			
			@Override
			public void mouseUp(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				int day = calendar.getDay();
				int month = calendar.getMonth();
				int year = calendar.getYear();
				
				Calendar c = Calendar.getInstance();
				c.set(calendar.getYear(), calendar.getMonth(), calendar.getDay());
				int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
				updateCalendarGUI.updateCalendar(CalendarOption.MONTH, day, month, year, null, dayOfWeek);
			}
			
			@Override
			public void mouseDoubleClick(MouseEvent arg0) {
				// TODO Auto-generated method stub				
			}
		});
		
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}
